package cn.rongdao.jrkabao.ui.deposit;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frtools.ui.activity.FRStartActivity;
import com.mifengkong.frtools.util.FRCollection;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.mifengkong.frwidget.titlebar.TitleBarUIComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseAutoFinishActivity;
import cn.rongdao.jrkabao.bean.deposit.DepositBean;
import cn.rongdao.jrkabao.bean.deposit.GetDepositInfoBean;
import cn.rongdao.jrkabao.model.deposit.DepositModel;
import cn.rongdao.jrkabao.model.deposit.GetDepositInfoModel;
import cn.rongdao.jrkabao.ui.cashadvance.CashAdvanceSuccessActivity;
import cn.rongdao.jrkabao.ui.cashadvance.addcard.AddDepositCardActivity;
import cn.rongdao.jrkabao.ui.deposit.event.DepositAddCardEvent;
import cn.rongdao.jrkabao.ui.deposit.event.DepositSuccessEvent;
import cn.rongdao.jrkabao.ui.deposit.uicomponent.DepositUIComponent;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.NumberUtil;

/**
 * Created by jack on 2017/7/29
 */

public class DepositActivity extends BaseAutoFinishActivity implements View.OnClickListener {

    public static void start(Context context) {
        FRStartActivity.start(context, DepositActivity.class);
    }

    @BindView(R.id.ad_titlebar)
    TitleBarUIComponent titleBarUIComponent;
    @BindView(R.id.ad_rl_bank_card)
    RelativeLayout rlBankCard;
    @BindView(R.id.ad_iv_bank_avatar)
    ImageView ivBankAvatar;
    @BindView(R.id.ad_tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.ad_tv_bank_number)
    TextView tvBankNumber;
    @BindView(R.id.ad_deposit_money)
    DepositUIComponent depositUIComponent;
    @BindView(R.id.ad_tv_confirm_deposit)
    TextView tvConfirmDeposit;

    private float minMoney;
    private String ruleUrl;
    private String canApply;

    @Override
    protected void initView() {
        super.initView();
        setAutoHideSoftInput(true);
        initTitleBar();
        initData();
        initListener();
    }

    private void initData() {
        GetDepositInfoModel.getInstance().execute(new FRBaseObserver<GetDepositInfoBean>(this, "加载中，请稍候...") {
            @Override
            protected void onBaseNext(GetDepositInfoBean data) {
                rlBankCard.setVisibility(View.VISIBLE);
                depositUIComponent.setVisibility(View.VISIBLE);
                tvConfirmDeposit.setVisibility(View.VISIBLE);
                tvBankName.setText(data.getBank_name());
                if (!FRString.isEmpty(data.getBank_number())) {
                    tvBankNumber.setText(FRCollection.appendAsString("尾号", data.getBank_number().substring(data.getBank_number().length() - 4), "储蓄卡", ""));
                }
                FRImageLoad.loadImage(data.getBank_icon(), ivBankAvatar,R.drawable.icon_no_distinguish_bank);
                depositUIComponent.setBalance(data.getBalance());
                depositUIComponent.setMaxMoney(data.getMax());
                minMoney = Float.valueOf(data.getMin());
                ruleUrl = data.getRule_url();
                canApply = data.getCan_apply();
            }
        });
    }

    private void initListener() {
        rlBankCard.setOnClickListener(this);
        tvConfirmDeposit.setOnClickListener(this);
    }

    private void goDeposit() {
        DepositModel.getInstance().execute(FRString.valueOf(depositUIComponent.getMoney()), new FRBaseObserver<DepositBean>(this, "正在提现,请稍候...") {
            @Override
            protected void onBaseNext(DepositBean data) {
                if (!FRString.isEmpty(data.getFlow_no())) {
                    EventBus.getDefault().post(new DepositSuccessEvent());
                    CashAdvanceSuccessActivity.start(thisActivity(), data.getFlow_no(), true);
                }
            }
        });
    }

    private void initTitleBar() {
        titleBarUIComponent.initTitle("提现");
        titleBarUIComponent.setBackgroundColor(ContextCompat.getColor(thisActivity(), R.color.colorPrimary));
        titleBarUIComponent.initLeftBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleBarUIComponent.initRight(R.drawable.icon_question_mark, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(thisActivity(), ruleUrl);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void depositAddCardEventMessage(DepositAddCardEvent depositAddCardEvent) {
        initData();
    }

    @Override
    protected int layoutRes() {
        return R.layout.activity_deposit;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ad_rl_bank_card:
                AddDepositCardActivity.start(thisActivity(), false, true);
                break;
            case R.id.ad_tv_confirm_deposit:
                if (!CommonUtil.isStatusSuccess(canApply)) {
                    FRToast.showToastSafe("您今日已提现，请明天再来哦");
                    return;
                }
                if (!depositUIComponent.isMoneyCorrect()) {
                    return;
                }
                if (depositUIComponent.getMoney() < minMoney) {
                    FRToast.showToastSafe(FRCollection.appendAsString("单笔提现金额不能少于", NumberUtil.getTwoDecimalPlaces(minMoney), "元", ""));
                    return;
                }
                goDeposit();
                break;
        }
    }
}